<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页</title>
<link href="../css/table.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.8.3.min.js"></script>
<script>
var rows={'pdProductname':'产品名称','norm':'规格','unit':'单位','pdPrice':'价格','pdAllcount':'库存','pdOther':'其它'};
var data =[];
var pageSize = 10;
var pageCount = 0;
var pageIndex =1;
var rowCount=0;

		$(function(){
			loadPage();
			$('#pageSize_num').change(function(){
				pageIndex=1;
				pageSize=$('#pageSize_num').val();
				if(pageSize<1 || isNaN(pageSize)){
					pageSize=1;
					$('#pageSize_num').val('1');
				}
				loadPage();	
			});
		});
		
		function reg(){
			$('#nextPage').click(function(){
				pageIndex=pageIndex+1;	
				loadPage();
			});
			$('#upPage').click(function(){
				pageIndex=pageIndex-1;	
				loadPage();
			});	
			$('.page_').click(function(){
				if(!isNaN(this.id)){
					pageIndex=this.id;
					loadPage();
				}
			});
			$("#checkAll").change(function(){
				var ck=document.getElementById("checkAll").checked;
		    	var ckList=document.getElementsByName("check");
		    	for ( var i = 0; i < ckList.length; i++) {
		    		ckList[i].checked=ck;
				}
			});
			$("input[name='check']").click(function(){
				var ck=true;
		    	var ckList=document.getElementsByName("check");
		    	for ( var i = 0; i < ckList.length; i++) {
		    		ck=ck&&ckList[i].checked;
				}
		    	document.getElementById("checkAll").checked=ck;
			});
		}
		
		function loadPage(){
			var proName=$("input[name='proName']").val();
	    	var minPrice=$("input[name='minPrice']").val();
	    	var maxPrice=$("input[name='maxPrice']").val();
	    	var minCount=$("input[name='minCount']").val();
	    	var maxCount=$("input[name='maxCount']").val();
	    	var norm=$("#norm").val();
	    	var unit=$("#unit").val();
			$.post('searchProduct.action',{'currpage':pageIndex,'pagesize':pageSize,"proName":proName,"minPrice":minPrice,"maxPrice":maxPrice,"minCount":minCount,"maxCount":maxCount,"norm":norm,"unit":unit},function(res){
				data=res.data;
				pageSize=res.pageSize;
				pageCount=res.pageCount;
				pageIndex=res.currpage;
				rowCount=res.rowCount;
				loadTable();
			},'json');	
		}
		
		function loadTable(){
				$('#ttitle').html('');
				$('#ttitle').append('<td><input type="checkbox" name="checkAll" id="checkAll">全选</td>');
				for(var key in rows){
					$('#ttitle').append('<td>'+rows[key]+'</td>');
				}
				$('#ttitle').append('<td>操作</td>');
				$('#tbody').html('');
				for(var i=0;i<data.length;i++){
					var ttr ='<tr>';
					ttr+='<td><input type="checkbox" name="check"  value="'+data[i].pdProductid+'"></td>';
					for(var key in rows){
						ttr+='<td>'+data[i][key]+'</td>';
					}
					ttr+='<td><a href="fupdateProduct.action?proId='+data[i].pdProductid+'">修改</a>&nbsp;<span onclick="del(\''+data[i].pdProductid+'\');" >删除</span></td>';
					$('#tbody').append(ttr+'</tr>');
				}
				loadPageNumber();
		}
		
		function loadPageNumber(){
			var start =1;
			var end = pageCount;
			$('#pageIndex').html('');
			if(pageIndex>1){
				$('#pageIndex').append('<div class="page_" id="upPage">上一页</div>');
			}
			if(pageCount>9 && pageIndex>9){
				var start = pageIndex-4;
			}
			if(pageCount>(pageIndex+4)){
				 end = pageIndex+4;
			}else{
				end =pageCount;	
			}
			
			for(var i=start;i<=end;i++){
				if(pageIndex==i){
					$('#pageIndex').append('<div class="page_sel" title="当前页">'+i+'</div>');
				}else{
					$('#pageIndex').append('<div id="'+i+'" class="page_" title="第'+i+'页">'+i+'</div>');
				}
			}
			
			if(pageIndex<pageCount){
				$('#pageIndex').append('<div class="page_" id="nextPage">下一页</div>');
			}
			
			$('#page_info').html('共'+rowCount+'条  每页'+pageSize+'条 当前'+pageIndex+'/'+pageCount+'页');
			reg();
		}
		
		//excel导出
		function excel(){
	        	var str="";
	        	var ckList=document.getElementsByName("check");
	    		for ( var i = 0; i < ckList.length; i++) {
	    			if (ckList[i].checked==true) {
	            		str=str+ckList[i].value+",";
					}
	    		}
	    		location.href="showExcelByCheckProduct.action?str="+str;
		}
		
		//excel导出
		function excelAll(){
			var proName=$("input[name='proName']").val();
	    	var minPrice=$("input[name='minPrice']").val();
	    	var maxPrice=$("input[name='maxPrice']").val();
	    	var minCount=$("input[name='minCount']").val();
	    	var maxCount=$("input[name='maxCount']").val();
	    	var norm=$("#norm").val();
	    	var unit=$("#unit").val();
	        location.href="searchByExcelProduct.action?proName="+proName+"&minPrice="+minPrice+"&maxPrice="+maxPrice+"&minCount="+minCount+"&maxCount="+maxCount+"&norm="+norm+"&unit="+unit;
		}
		
		//删除
		 function del(id){
			 var result = confirm("确定删除吗?");
			 if (result) {
				 $.get("deleteProduct.action",{"proId":id},function(data){
						if (data=="true") {
							alert("删除成功！");
							location.reload(true);
						}else{
							alert("删除失败！");
						}
				});
			 }
	    }
		
</script>
</head>

<body>
<div>
	<s:form  action="searchProduct" method="post" theme="simple" id="sform">
	          <input id="showType" name="showType" type="hidden">
	          <input id="pageindex" name="pageIndex" type="hidden" value="1">
	              商品名:<input name="proName" size="20">
                       价格：<input name="minPrice" size="8" maxlength="8">-<input name="maxPrice" size="8">
	               库存:<input name="minCount" size="5">-<input name="maxCount" size="5">
	  规格:<s:select id="norm" name="norm" list="norms" listKey="typeId" listValue="typeValue" label="规格" theme="simple" ></s:select>
	  单位:<s:select id="unit" name="unit" list="units" listKey="typeId" listValue="typeValue" label="单位" theme="simple" ></s:select>
	 <input type="reset" value="  重置  "> <input type="button" value="  查询  " onclick="loadPage();"><input type="button" value="  导出查询结果  " onclick="excelAll();"><input type="button" onclick="excel();" value="  导出选中  ">
	 </s:form>
	</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<thead id="head">
  <tr id="ttitle">
  </tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<div id="page_index">
  <div id="page_info">共[10]条记录 每页5条 共2页</div>
  <div id="pageIndex"> 
    <div class="page_">1</div>
  </div>
  <div id="setPage">每页
    <label for="pageSize_num"></label>
    <input name="pageSize_num" type="text" id="pageSize_num" value="10" size="4" maxlength="4" />
  条</div>
</div>
</body>
</html>
